﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Testing_components;
using System.Collections.ObjectModel;

namespace BuildingTomorow
{
    public class PresentableSensor :INotifyPropertyChanged
    {

        private SensorDB _sensorRef;
        private bool _isSelected =false;
        private string _presentableValue;
        private double _value;
        private DateTime _lastfromdate;
        private DateTime _lasttodate;
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public PresentableSensor()
        {
            _sensorRef = new SensorDB();
            _presentableValue = "";
            _value = 0;

        }

        public PresentableSensor(SensorDB sen)
        {
            this._sensorRef = sen;
            SensorDataDB lastreadin = DataAccess.GetInstance().GetSensorLastReading(_sensorRef.ID);
            _presentableValue = lastreadin.Value+ _sensorRef.Measurment;
            _value = lastreadin.Value;
        }

        public void AddSensorDB(SensorDB sen)
        {
            this._sensorRef = sen;
            SensorDataDB lastreadin = DataAccess.GetInstance().GetSensorLastReading(_sensorRef.ID);
            _presentableValue = lastreadin.Value + _sensorRef.Measurment;
            _value = lastreadin.Value;
        }

        public int ID
        {
            get { return _sensorRef.ID; }
            set
            {
               if(_sensorRef.ID != value) _sensorRef.ID = value;
                OnPropertyChanged("ID");
                DataAccess.GetInstance().UpdateSensors();
            }
        }

        public string Name
        {
            get { return _sensorRef.Name; }
            set
            {
                if (_sensorRef.Name != value) _sensorRef.Name = value;
                OnPropertyChanged("Name");
                DataAccess.GetInstance().UpdateSensors();
            }
        }

        public string Description
        {
            get { return _sensorRef.Description; }
            set
            {
                if (_sensorRef.Description != value) _sensorRef.Description = value;
                OnPropertyChanged("Description");
                DataAccess.GetInstance().UpdateSensors();
            }
        }

        public string PresentableValue
        {
            get { return _presentableValue; }
            set
            {
                if (_presentableValue != value) _presentableValue= value;
                OnPropertyChanged("PresentableValue");
            }
        }

        public double Value
        {
            get { return _value; }
            set
            {
                if (_value != value) _value = value;
                OnPropertyChanged("Value");
            }
        }

        public string IconSource
        {
            get { return _sensorRef.Icon; }
            set
            {
                if (_sensorRef.Icon != value) _sensorRef.Icon = value;
                OnPropertyChanged("IconSource");
            }
        }

        public string Measurment
        {
            get { return _sensorRef.Measurment; }
            set
            {
                if (_sensorRef.Measurment != value) _sensorRef.Measurment = value;
                OnPropertyChanged("Measurment");
            }
        }
        public string Type
        {
            get { return _sensorRef.Type; }
            set
            {
                if (_sensorRef.Type != value) _sensorRef.Type = value;
                OnPropertyChanged("Type");
                DataAccess.GetInstance().UpdateSensors();
            }
        }

        public bool Status
        {
            get { return _sensorRef.Status; }
            set
            {
                if (_sensorRef.Status != value) _sensorRef.Status = value;
                OnPropertyChanged("Status");
                DataAccess.GetInstance().UpdateSensors();
            }
        }

        public bool IsSensorSelected
        {
            get { return _isSelected; }
            set
            {
                if (_isSelected != value) _isSelected = value;
                OnPropertyChanged("IsSensorSelected");
            }
        }

        public double X
        {
            get { return _sensorRef.X; }
            set
            {
                if (_sensorRef.X != value) _sensorRef.X = value;
                OnPropertyChanged("X");
            }
        }

        public double Y
        {
            get { return _sensorRef.Y; }
            set
            {
                if (_sensorRef.Y != value) _sensorRef.Y = value;
                OnPropertyChanged("Y");
            }
        }

        public double Z
        {
            get { return _sensorRef.Z; }
            set
            {
                if (_sensorRef.Z != value) _sensorRef.Z = value;
                OnPropertyChanged("Z");
            }
        }

        public ObservableCollection<PresentableDataPoint> DataPoints
        {
            get;
            set;
        }

        public void UpdateData(DateTime fromTime, DateTime toTime)
        {
            if (DataPoints == null || DataPoints.Count == 0)
            {
                DataPoints = new ObservableCollection<PresentableDataPoint>(DataAccess.GetInstance().GetPresentableSensorReadings(ID, fromTime, toTime));
                _lastfromdate = fromTime;
                _lasttodate = toTime;           
            }
            if (_lastfromdate.CompareTo(fromTime) != 0 || _lasttodate.CompareTo(toTime) != 0)
            {
                DataPoints = new ObservableCollection<PresentableDataPoint>(DataAccess.GetInstance().GetPresentableSensorReadings(ID, fromTime, toTime));
                _lastfromdate = fromTime;
                _lasttodate = toTime;  
            }


        }
    }
}
